package com.hns.can.master.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysCodeDscEntity;
import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.base.service.SysUserService;
import com.hns.can.common.entity.BaseEntity;
import com.hns.can.common.web.BaseController;
import com.hns.can.master.entity.BaseOrganEntity;
import com.hns.can.master.service.BaseOrganService;
import com.hns.can.master.vo.LeftOrgVo;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.SearchConditionVo;

/**
 * 组织机构机构相关控制类
 * @author:zhangyu
 * @email:zhangyu2222z@sina.cn
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/baseOrgan")
public class BaseOrganController extends BaseController {
    
	/**
	 * 机构信息service
	 */
    @Autowired
    private BaseOrganService baseOrganService; // 注入baseOrganService
    /**
     * 用户信息service
     */
    @Autowired
    private SysUserService   sysUserService;  // 注入sysUserService
                                               
    
    /**
     * 得到组织机构的全部数据，用来展示树
     * @return 组织机构数据
     * @throws Exception
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/getBaseOrganList")
    @ResponseBody
    public Object getBaseOrganList() throws Exception {
        List<BaseEntity> list = baseOrganService.getBaseOrganList(this.getCurrentUser().getUser());
        return this.getReturnResult(list);
    }
    
    /**
     * 设备信息页面的组织机构树
     * @throws Exception
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/getBaseOrganListForEquipment")
    @ResponseBody
    public Object getBaseOrganListForEquipment() throws Exception {
        List<BaseEntity> list = baseOrganService.getBaseOrganListForEquipment(this.getCurrentUser().getUser());
        return this.getReturnResult(list);
    }
    
    /**
     * @param entity
     * @param response
     * @throws Exception
     * @author:JeffXu
     * @email:835264047@qq.com
     * @创建日期:2014-6-11
     * @功能说明：修改组织机构
     */
    @RequestMapping("/updateBaseOrgan")
    @ResponseBody
    public Object updateBaseOrgan(BaseOrganEntity entity) throws Exception {
		baseOrganService.updateBaseOrgan(entity, this.getCurrentUser().getUser());
    	return this.getReturnResult(entity.getOrganId());
    }
    
    /**
     * 新增一个组织机构
     * @param entity 组织机构信息
     * @return 操作返回信息
     * @throws Exception
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/saveBaseOrgan")
    @ResponseBody
    public Object saveBaseOrgan(BaseOrganEntity entity) throws Exception {
        String organId = baseOrganService.saveBaseOrgan(entity, this.getCurrentUser().getUser());
        return this.getReturnResult(organId);
    }
    
    /** 
     * 删除一个组织机构
     * @param organId 机构id
     * @return 操作返回信息
     * @throws Exception
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/delBaseOrgan")
    @ResponseBody
    public Object delBaseOrgan(@RequestParam("organId") String organId) throws Exception {
        baseOrganService.delBaseOrgan(organId, this.getCurrentUser().getUser());
        return this.getReturnResult();
    }
    
    /**
     * 获取左侧机构、线、车
     * @return 左侧机构树信息
     * @throws Exception
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/getLeftOrg4Car")
    @ResponseBody
    public Object getLeftOrg4Car() throws Exception {
        // List<SysRoleEntity> roleIdList = (List<SysRoleEntity>)
        // request.getSession().getAttribute("SES_ROLELIST");
        // if(PubUtil.isEmpty(roleIdList)){
        // ResponseUtil.responseJSON(null, response);
        // }else{
        // List<LeftOrgVo> list = baseOrganService.getLeftOrg4Car(roleIdList);
        // //如果没有配置数据权限，返回null
        // if(PubUtil.isEmpty(list) || list.get(0).getSno().equals("0")){
        // ResponseUtil.responseJSON(null, response);
        // }
        // ResponseUtil.responseJSON(list, response);
        // }
        // 2015-03-17 modify by qianjy
        SysUserEntity user = this.getCurrentUser().getUser();
        if (PubUtil.isEmpty(user)) {
        	throw new BusinessException(MsgType.WARN.getFlag(),"未知用户");
        } else {
            SearchConditionVo searchVo = new SearchConditionVo();
            searchVo.setUserid(user.getId());
            List<LeftOrgVo> list = baseOrganService.getLeftOrg4Car(searchVo);
            // 如果没有配置数据权限，返回null
            if (PubUtil.isEmpty(list) || "0".equals(list.get(0).getSno())) {
                throw new BusinessException(MsgType.WARN.getFlag(),"数据为空");
            }
            return this.getReturnResult(list);
        }
    }
    
    /**
     * 获取左侧机构、线、车、驾驶员
     * @return 左侧机构树信息
     * @throws Exception
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/getLeftOrg4Driver")
    @ResponseBody
    public Object getLeftOrg4Driver() throws Exception {
        // List<SysRoleEntity> roleIdList = (List<SysRoleEntity>)
        // request.getSession().getAttribute("SES_ROLELIST");
        // if(PubUtil.isEmpty(roleIdList)){
        // ResponseUtil.responseJSON(null, response);
        // }else{
        // List<LeftOrgVo> list =
        // baseOrganService.getLeftOrg4Driver(roleIdList);
        // //如果没有配置数据权限，返回null
        // if(PubUtil.isEmpty(list) || list.get(0).getSno().equals("0")){
        // ResponseUtil.responseJSON(null, response);
        // }
        // ResponseUtil.responseJSON(list, response);
        // }
        // 2015-03-17 modify by qianjy
        SysUserEntity user = this.getCurrentUser().getUser();
        if (PubUtil.isEmpty(user)) {
        	throw new BusinessException(MsgType.WARN.getFlag(), "未知用户");
        } else {
            SearchConditionVo searchVo = new SearchConditionVo();
            searchVo.setUserid(user.getId());
            List<LeftOrgVo> list = baseOrganService.getLeftOrg4Driver(searchVo);
            // 如果没有配置数据权限，返回null
//            if (PubUtil.isEmpty(list) || "0".equals(list.get(0).getSno())) {
            if (PubUtil.isEmpty(list)) {
            	throw new BusinessException(MsgType.WARN.getFlag(),"数据为空");
            }
            return this.getReturnResult(list);
        }
    }
    
    /**
     * 获取左侧机构、线
     * @return 左侧机构树信息
     * @throws Exception
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/getLeftOrg4Line")
    @ResponseBody
    public Object getLeftOrg4Line() throws Exception {
        // List<SysRoleEntity> roleIdList = (List<SysRoleEntity>)
        // request.getSession().getAttribute("SES_ROLELIST");
        // if(PubUtil.isEmpty(roleIdList)){
        // ResponseUtil.responseJSON(null, response);
        // }else{
        // List<LeftOrgVo> list = baseOrganService.getLeftOrg4Line(roleIdList);
        // //如果没有配置数据权限，返回null
        // if(PubUtil.isEmpty(list) || list.get(0).getSno().equals("0")){
        // ResponseUtil.responseJSON(null, response);
        // }
        // ResponseUtil.responseJSON(list, response);
        // }
        // 2015-03-17 modify by qianjy
        SysUserEntity user = this.getCurrentUser().getUser();
        if (PubUtil.isEmpty(user)) {
        	throw new BusinessException(MsgType.WARN.getFlag(),"未知用户");
        } else {
            SearchConditionVo searchVo = new SearchConditionVo();
            searchVo.setUserid(user.getId());
            List<LeftOrgVo> list = baseOrganService.getLeftOrg4Line(searchVo);
            // 如果没有配置数据权限，返回null
            if (PubUtil.isEmpty(list) || "0".equals(list.get(0).getSno())) {
            	throw new BusinessException(MsgType.WARN.getFlag(),"数据为空");
            }
            return this.getReturnResult(list);
        }
    }
    
    /**
     * 校验机构资源权限
     * @param organId 机构id
     * @param response
     * @throws Exception
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/getValidateOrganInRes")
    @ResponseBody
    public Object getValidateOrganInRes(@RequestParam("organId") String organId) throws Exception {
        return this.getReturnResult(baseOrganService.validateOrganInRes(organId));
    }
    
    /**
     * 取得组织机构树
     * @return 机构树
     * @throws Exception
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/getOrganTree")
    @ResponseBody
    public Object getOrganTree() throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        List<BaseOrganEntity> list = baseOrganService.getOrganTree(user.getId(), user.getOrganId());
        return this.getReturnResult(list);
    }
    
    /**
     * 根据省id获取对应市信息
     * @param ProvinceId 省id
     * @return 市信息
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/getCityByProvinceId")
    @ResponseBody
    public Object getCityByProvinceId(String provinceId) throws Exception {
        List<SysCodeDscEntity> list = baseOrganService.getCityByProvinceId(provinceId);
        return this.getReturnResult(list);
    }
}
